package com.smart.community.commons.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * OpenAPI配置类
 * @author Wu.Liang
 * @since 2024-12-19
 * @version 1.0.0
 */
@Slf4j
@Configuration
@ConditionalOnClass(name = "io.swagger.v3.oas.models.OpenAPI")
@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true)
public class OpenApiConfig {
    
    @Value("${spring.application.name:智慧社区微服务}")
    private String applicationName;
    
    @Value("${spring.application.version:1.0.0}")
    private String applicationVersion;
    
    @Value("${server.port:8080}")
    private String serverPort;
    
    @Value("${server.servlet.context-path:}")
    private String contextPath;
    
    /**
     * OpenAPI配置
     * 
     * @return OpenAPI
     */
    @Bean
    @ConditionalOnMissingBean
    public OpenAPI openAPI() {
        log.info("🔧 初始化OpenAPI配置...");
        
        OpenAPI openAPI = new OpenAPI();
        
        // 设置基本信息
        openAPI.info(apiInfo());
        
        // 设置服务器信息
        openAPI.servers(apiServers());
        
        // 设置安全方案
        openAPI.addSecurityItem(securityRequirement());
        
        // 设置安全方案组件
        openAPI.components(apiComponents());
        
        log.info("✅ OpenAPI配置初始化完成");
        return openAPI;
    }
    
    /**
     * API信息
     * 
     * @return Info
     */
    private Info apiInfo() {
        return new Info()
                .title(applicationName + " API文档")
                .description("智慧社区微服务系统API接口文档")
                .version(applicationVersion)
                .contact(apiContact())
                .license(apiLicense());
    }
    
    /**
     * API联系信息
     * 
     * @return Contact
     */
    private Contact apiContact() {
        return new Contact()
                .name("智慧社区开发团队")
                .email("dev@smart-community.com")
                .url("https://www.smart-community.com");
    }
    
    /**
     * API许可证信息
     * 
     * @return License
     */
    private License apiLicense() {
        return new License()
                .name("MIT License")
                .url("https://opensource.org/licenses/MIT");
    }
    
    /**
     * API服务器信息
     * 
     * @return 服务器列表
     */
    private List<Server> apiServers() {
        Server localServer = new Server()
                .url("http://localhost:" + serverPort + contextPath)
                .description("本地开发环境");
        
        Server devServer = new Server()
                .url("https://dev-api.smart-community.com" + contextPath)
                .description("开发环境");
        
        Server prodServer = new Server()
                .url("https://api.smart-community.com" + contextPath)
                .description("生产环境");
        
        return java.util.Arrays.asList(localServer, devServer, prodServer);
    }
    
    /**
     * 安全要求
     * 
     * @return SecurityRequirement
     */
    private SecurityRequirement securityRequirement() {
        return new SecurityRequirement().addList("Bearer Authentication");
    }
    
    /**
     * API组件
     * 
     * @return io.swagger.v3.oas.models.Components
     */
    private io.swagger.v3.oas.models.Components apiComponents() {
        return new io.swagger.v3.oas.models.Components()
                .addSecuritySchemes("Bearer Authentication", bearerAuth());
    }
    
    /**
     * Bearer认证方案
     * 
     * @return SecurityScheme
     */
    private SecurityScheme bearerAuth() {
        return new SecurityScheme()
                .type(SecurityScheme.Type.HTTP)
                .scheme("bearer")
                .bearerFormat("Sa-Token")
                .description("Sa-Token认证令牌");
    }
    
    /**
     * 获取应用名称
     * 
     * @return 应用名称
     */
    public String getApplicationName() {
        return applicationName;
    }
    
    /**
     * 获取应用版本
     * 
     * @return 应用版本
     */
    public String getApplicationVersion() {
        return applicationVersion;
    }
    
    /**
     * 获取服务器端口
     * 
     * @return 服务器端口
     */
    public String getServerPort() {
        return serverPort;
    }
    
    /**
     * 获取上下文路径
     * 
     * @return 上下文路径
     */
    public String getContextPath() {
        return contextPath;
    }
}
